#define LOG_TAG "main_imp"

#include <log/log.h>
#include "HandlerThread.h"
#include <unistd.h>
#include <utils/Looper.h>
#include"handler.h"
namespace android
{
    class MainMessageHandler;
    bool mShouldQuit;
    sp<Looper> threadLooper;
    sp<Looper> mainLooper;
    sp<MainMessageHandler> mainMessageHandler;

    class ThreadMessageHandler : public MessageHandler
    {
    public:
        virtual void handleMessage(const Message &message)
        {
            ALOGD("ThreadMessageHandler message->what = %d  %d", message.what,message.arg1);
            // do something what you want
            //usleep(10 * 1000);
            // HandlerThread程调用mainLooper的sendMessage，发送一个Message()到主线程
            //mainLooper->sendMessage(mainMessageHandler, Message(message.what));
        }
    };

    class MainMessageHandler : public MessageHandler
    {
    public:
        virtual void handleMessage(const Message &message)
        {
            ALOGD("MainMessageHandler message->what = %d", message.what);
        }
    };

    int test()
    {
        ALOGD("start test");
        // 开启`HandlerThread`线程
        sp<HandlerThread> mHandlerThread = new HandlerThread();
        ALOGD("HandlerThread start");
        mHandlerThread->start("VMS.NATIVE_LOOP");
        // 从线程中获取`threadLooper`对象
        threadLooper = mHandlerThread->getLooper();
        // new ThreadMessageHandler()里面重载了handleMessage(),会在消息来后回调
        sp<ThreadMessageHandler> threadMessageHandler = new ThreadMessageHandler();
        // 主线程调用threadLooper的sendMessage，发送一个Message(1)到HandlerThread线程
        threadLooper->sendMessage(threadMessageHandler, Message(1));
        // 发送一个延时消息
        threadLooper->sendMessageDelayed(ms2ns(100), threadMessageHandler, Message(2));
        threadLooper->sendMessage(threadMessageHandler, Message(3));

        // 主线程构造一个mainLooper
       // mainLooper = Looper::prepare(0);
        // new MainMessageHandler()里面重载了handleMessage(),会在消息来后回调
      //  mainMessageHandler = new MainMessageHandler();
        // 主线程调用mainLooper的sendMessage，发送一个Message(4)到自己线程
       // mainLooper->sendMessage(mainMessageHandler, Message(4));
        // 主线程调用mainLooper的sendMessageDelayed，发送一个延时Message(5)到自己线程
       // mainLooper->sendMessageDelayed(ms2ns(1000), mainMessageHandler, Message(5));

        // 主线程开启循环等待消息
		int i=0;
        while (true)
        {
			ALOGD("send message->what = %d   %lld",i,ms2ns(1000));
            threadLooper->sendMessageDelayed(ms2ns(1000), threadMessageHandler, Message(i,i));
            i++;
			usleep(100 * 1000);
        }
        // 结束HandlerThread
        //mHandlerThread->quit();
        ALOGD("test end");
        return 0;
    }
} // namespace android

void callback(int what,int a,int c,const char *data){
	
	 ALOGD("recive message->what = %d %s",what,data);
}
int main(int argc, char const *argv[])
{
    //android::test();
	void *p=handler_init(callback);
	int i=1;
	  while (true)
        {
			
			 ALOGD("send message->what = %d ",i);
		    send_Message(p,i,i,(char *)"data");
			send_Message_Delay(p,i,i,(char *)"data",1000);
			usleep(100 * 1000);
		}
    return 0;
}
